package com.sanjiahantang.ythshopping.utils;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.model.DillModel;
import com.sanjiahantang.shopping.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.shopping.uilibrary.adapter.listview.QuickAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/7/25.
 */

public class DillWindow extends PopupWindow {

    Activity context;
    List<DillModel.DillType> list;
    List<String> data;
    ListView lv_list;
    DillPopCallBack back;

    public DillWindow(Activity context, List<DillModel.DillType> list, View parent,DillPopCallBack back)
    {
        this.context=context;
        this.list=list;
        this.data=new ArrayList<>();
        for (DillModel.DillType item:list)
        {
            this.data.add(item.getDisplay());
        }
        this.back=back;
        InitView(parent);
    }

    public DillWindow(Activity context,List<DillModel.DillType> list,List<String> data,View parent,DillPopCallBack back)
    {
        this.context=context;
        this.data=data;
        this.back=back;
        InitView(parent);
    }

    public void InitView(View parent)
    {
        LayoutInflater inflater=LayoutInflater.from(context);
        View view=inflater.inflate(R.layout.dill_list_pop,null);
        int h=context.getWindowManager().getDefaultDisplay().getHeight();
        int w=context.getWindowManager().getDefaultDisplay().getWidth();
        this.setContentView(view);
        this.setWidth(parent.getWidth());
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setFocusable(false);
        this.setOutsideTouchable(true);
        this.update();
        ColorDrawable dw=new ColorDrawable(0000000000);
        this.setBackgroundDrawable(dw);
        //this.setAnimationStyle(R.style.Ani);

        lv_list= (ListView) view.findViewById(R.id.lv_dill_pop);
        QuickAdapter<String> adapter=new QuickAdapter<String>(context,R.layout.text_item,data) {
            @Override
            protected void convert(BaseAdapterHelper helper, String item) {
                helper.setText(R.id.txtName,item);
            }
        };
        lv_list.setAdapter(adapter);
        lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                back.callBack(data.get(i),i);
                DillWindow.this.dismiss();
            }
        });

    }
    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
        } else {
            this.dismiss();
        }
    }

    public static interface DillPopCallBack
    {
        void callBack(String title,int position);
    }



}
